package com.wyh.service.serviceImpl.groupOne.customer;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wyh.common.constant.ProjectConstant;
import com.wyh.controller.groupOne.projectabout.utils.NewTime;
import com.wyh.domain.dto.groupOne.customer.ResourceDTO;
import com.wyh.domain.po.feemanagementpo.Organization;
import com.wyh.domain.po.groupOne.organization.MasterDataOrganization;
import com.wyh.domain.po.groupOne.projectabout.BuildingunitsPO;
import com.wyh.domain.po.groupOne.projectabout.LayerPO;
import com.wyh.domain.po.groupOne.projectabout.OrganizationPO;
import com.wyh.domain.po.groupOne.projectabout.ProjectPO;
import com.wyh.domain.vo.groupOne.customer.ResourceVO;
import com.wyh.mapper.groupOne.customer.MasterDataCResourceMapper;
import com.wyh.mapper.groupOne.organization.MasterDataOrganizationMapper;
import com.wyh.service.groupOne.customer.MasterDataCResourceService;
import com.wyh.service.groupOne.projectabout.MasterDataProjectBuildingService;
import com.wyh.service.groupOne.projectabout.MasterDataProjectLayerService;
import com.wyh.service.groupOne.projectabout.MasterDataProjectOrganizationService;
import com.wyh.service.groupOne.projectabout.MasterDataProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
public class MasterDataCResourceServiceImpl implements MasterDataCResourceService {
    @Autowired
    private MasterDataCResourceMapper resourceMapper;

    @Autowired
    private MasterDataProjectService masterDataProjectService;
    @Autowired
    private MasterDataProjectLayerService masterDataProjectLayerService;
    @Autowired
    private MasterDataProjectOrganizationService masterDataProjectOrganizationService;

    @Autowired
    MasterDataProjectBuildingService masterDataProjectBuildingService;


    @Override
    public List<ResourceVO> selectByCustomerId(Integer id) throws Exception {
        List<ResourceDTO> resourceDTOS = resourceMapper.selectByCustomerId(id);
        ArrayList<ResourceVO> resourceVO = new ArrayList<>();
        for (ResourceDTO resourceDTO : resourceDTOS) {
            System.out.println(resourceDTOS+"11111111111111111111111111");
            String Role = new String();
            String Type = new String();
            LayerPO layerPO = masterDataProjectLayerService.getOne(new QueryWrapper<LayerPO>()
                    .eq("id", resourceDTO.getBuildingid()));
            ResourceDTO resourceDTO1 = resourceMapper.selectByCustomerAndResoure(id,resourceDTO.getResourceid());
            BuildingunitsPO buildingunitsPO = masterDataProjectBuildingService.getOne(new QueryWrapper<BuildingunitsPO>()
                    .eq("id", layerPO.getParentId()));
            if (buildingunitsPO.getIsconnected().equals(ProjectConstant.PROJECT_Y)) {
                ProjectPO projectPO = masterDataProjectService.getOne(new QueryWrapper<ProjectPO>()
                        .eq("id", buildingunitsPO.getParentid()));
                if (projectPO.getIsconnected().equals(ProjectConstant.PROJECT_Y)) {
                    OrganizationPO organizationPO = masterDataProjectOrganizationService.getOne(new QueryWrapper<OrganizationPO>()
                            .eq("id", projectPO.getSuperiorid()));
                    resourceDTO.setProjectAll(projectPO.getName() + "/"
                            + buildingunitsPO.getName() + "/"
                            + layerPO.getName());

                    List<OrganizationPO> organizationList = getSuperiorOrganizations(organizationPO);
//                    List<OrganizationPO> organizationList = masterDataProjectOrganizationService.getOrganizationList(organizationPO.getId());\

                    String OrganizationAll = "";
                    for (OrganizationPO po : organizationList) {
                        OrganizationAll += po.getName() + "/";
                        resourceDTO.setOrganizationAll(OrganizationAll);
                    }
                } else {
                    ProjectPO projectPO1 = masterDataProjectService.getOne(new QueryWrapper<ProjectPO>()
                            .eq("id", projectPO.getSuperiorid()));
                    OrganizationPO organizationPO = masterDataProjectOrganizationService.getOne(new QueryWrapper<OrganizationPO>()
                            .eq("id", projectPO1.getSuperiorid()));
                    resourceDTO.setProjectAll(projectPO1.getName() + "/" + projectPO.getName() + "/"
                            + buildingunitsPO.getName() + "/"
                            + layerPO.getName());
//                    List<OrganizationPO> organizationList = masterDataProjectOrganizationService.getOrganizationList(organizationPO.getId());
                    List<OrganizationPO> organizationList = getSuperiorOrganizations(organizationPO);
                    String OrganizationAll = "";
                    for (OrganizationPO po : organizationList) {
                        OrganizationAll += po.getName() + "/";
                        resourceDTO.setOrganizationAll(OrganizationAll);
                    }
                }
            }
            if (buildingunitsPO.getIsconnected().equals(ProjectConstant.PROJECT_N)) {
                BuildingunitsPO buildingunitsPO1 = masterDataProjectBuildingService.getOne(new QueryWrapper<BuildingunitsPO>()
                        .eq("id", buildingunitsPO.getParentid()));
                if (buildingunitsPO1.getIsconnected().equals(ProjectConstant.PROJECT_N)) {
                    BuildingunitsPO buildingunitsPO2 = masterDataProjectBuildingService.getOne(new QueryWrapper<BuildingunitsPO>()
                            .eq("id", buildingunitsPO1.getParentid()));

                    ProjectPO projectPO = masterDataProjectService.getOne(new QueryWrapper<ProjectPO>()
                            .eq("id", buildingunitsPO2.getParentid()));
                    if (projectPO.getIsconnected().equals(ProjectConstant.PROJECT_Y)) {
                        OrganizationPO organizationPO = masterDataProjectOrganizationService.getOne(new QueryWrapper<OrganizationPO>()
                                .eq("id", projectPO.getSuperiorid()));
                        resourceDTO.setProjectAll(projectPO.getName() + "/" + buildingunitsPO2.getName() + "/" + buildingunitsPO1.getName() + "/"
                                + buildingunitsPO.getName() + "/"
                                + layerPO.getName());
//                        List<OrganizationPO> organizationList = masterDataProjectOrganizationService.getOrganizationList(organizationPO.getId());
                        List<OrganizationPO> organizationList = getSuperiorOrganizations(organizationPO);
                        String OrganizationAll = "";
                        for (OrganizationPO po : organizationList) {
                            OrganizationAll += po.getName() + "/";
                            resourceDTO.setOrganizationAll(OrganizationAll);
                        }
                    } else {
                        ProjectPO projectPO1 = masterDataProjectService.getOne(new QueryWrapper<ProjectPO>()
                                .eq("id", projectPO.getSuperiorid()));
                        OrganizationPO organizationPO = masterDataProjectOrganizationService.getOne(new QueryWrapper<OrganizationPO>()
                                .eq("id", projectPO1.getSuperiorid()));
                        resourceDTO.setProjectAll(projectPO1.getName() + "/" + projectPO.getName() + "/" + buildingunitsPO2.getName() + "/" + buildingunitsPO1.getName() + "/"
                                + buildingunitsPO.getName() + "/"
                                + layerPO.getName());
//                        List<OrganizationPO> organizationList = masterDataProjectOrganizationService.getOrganizationList(organizationPO.getId());
                        List<OrganizationPO> organizationList = getSuperiorOrganizations(organizationPO);
                        String OrganizationAll = "";
                        for (OrganizationPO po : organizationList) {
                            OrganizationAll += po.getName() + "/";
                            resourceDTO.setOrganizationAll(OrganizationAll);
                        }
                    }

                } else {
                    ProjectPO projectPO = masterDataProjectService.getOne(new QueryWrapper<ProjectPO>()
                            .eq("id", buildingunitsPO1.getParentid()));
                    if (projectPO.getIsconnected().equals(ProjectConstant.PROJECT_Y)) {
                        OrganizationPO organizationPO = masterDataProjectOrganizationService.getOne(new QueryWrapper<OrganizationPO>()
                                .eq("id", projectPO.getSuperiorid()));
                        resourceDTO.setProjectAll(projectPO.getName() + "/" + buildingunitsPO1.getName() + "/"
                                + buildingunitsPO.getName() + "/"
                                + layerPO.getName());
//                        List<OrganizationPO> organizationList = masterDataProjectOrganizationService.getOrganizationList(organizationPO.getId());
                        List<OrganizationPO> organizationList = getSuperiorOrganizations(organizationPO);
                        String OrganizationAll = "";
                        for (OrganizationPO po : organizationList) {
                            OrganizationAll += po.getName() + "/";
                            resourceDTO.setOrganizationAll(OrganizationAll);
                        }
                    } else {
                        ProjectPO projectPO1 = masterDataProjectService.getOne(new QueryWrapper<ProjectPO>()
                                .eq("id", projectPO.getSuperiorid()));
                        OrganizationPO organizationPO = masterDataProjectOrganizationService.getOne(new QueryWrapper<OrganizationPO>()
                                .eq("id", projectPO1.getSuperiorid()));
                        resourceDTO.setProjectAll(projectPO1.getName() + "/" + projectPO.getName() + "/" + buildingunitsPO1.getName() + "/"
                                + buildingunitsPO.getName() + "/"
                                + layerPO.getName());
//                        List<OrganizationPO> organizationList = masterDataProjectOrganizationService.getOrganizationList(organizationPO.getId());
                        List<OrganizationPO> organizationList = getSuperiorOrganizations(organizationPO);
                        String OrganizationAll = "";
                        for (OrganizationPO po : organizationList) {
                            OrganizationAll += po.getName() + "/";
                            resourceDTO.setOrganizationAll(OrganizationAll);
                        }
                    }
                }
                resourceDTO.setType(resourceDTO1.getType())
                        .setNumber(resourceDTO1.getNumber())
                        .setName(resourceDTO1.getName())
                        .setInsidearea(resourceDTO1.getInsidearea())
                        .setBuildingarea(resourceDTO1.getBuildingarea());
            }
            String s = NewTime.cstTurnTime(resourceDTO.getGenerationtime().toString());
            if (resourceDTO.getRole().equals(0)) {
                Role = "业主";
            }
            if (resourceDTO.getRole().equals(1)) {
                Role = "租户";
            }
            if (resourceDTO.getType().equals(0)) {
                Type = "房屋";
            }
            if (resourceDTO.getType().equals(1)) {
                Type = "车位";
            }
            ResourceVO resource = new ResourceVO()
                    .setOrganizationAll(resourceDTO.getOrganizationAll()
                            .substring(0,resourceDTO.getOrganizationAll().length()-1))
                    .setBuildingarea(resourceDTO.getBuildingarea())
                    .setCreatetime(s)
                    .setInsidearea(resourceDTO.getInsidearea())
                    .setRole(Role)
                    .setType(Type)
                    .setProjectAll(resourceDTO.getProjectAll())
                    .setName(resourceDTO.getName())
                    .setNumber(resourceDTO.getNumber());

            resourceVO.add(resource);
        }
        return resourceVO;
    }
    private List<OrganizationPO> getSuperiorOrganizations(OrganizationPO organization) {
        List<OrganizationPO> result = new ArrayList<>();
        result.add(organization);
        while (organization.getSuperiorid() != null && organization.getSuperiorid() != 0) {
            organization =masterDataProjectOrganizationService.getOne(new QueryWrapper<OrganizationPO>()
                    .eq("id",  organization.getSuperiorid()));
            result.add(organization);
        }
        Collections.reverse(result);  // 将结果反转以便从顶级组织开始
        return result;
    }


}
